package com.xunshangwang.pinhaohuo.adapters;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xunshangwang.pinhaohuo.R;
import com.xunshangwang.pinhaohuo.entity.Order;
import com.xunshangwang.pinhaohuo.entity.Pin;
import com.xunshangwang.pinhaohuo.ui.activities.GroupDetailActivity;
import com.xunshangwang.pinhaohuo.ui.dialogs.LogisticsDialog;
import com.xunshangwang.pinhaohuo.ui.fragments.BaseFragment;
import com.xunshangwang.pinhaohuo.widgets.LoadLayout;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import tool.library.adapter.BaseRecyclerAdapter;

/**
 * Created by xzp on 16/3/10.
 */
public class OrderAdapter extends BaseRecyclerAdapter<Order>{
    private SimpleDateFormat mDateFormat;
    private DecimalFormat mPriceFormat;
    private String[] mStatusArray;
    private OnFunctionClickListener mFunctionClickListener;
    public OrderAdapter(Context context) {
        super(context);
        mDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        mPriceFormat=new DecimalFormat("0.00");
        mStatusArray=context.getResources().getStringArray(R.array.order_status_array);
    }

    @Override
    public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(getContext()).inflate(R.layout.order_item,parent,false);
        return new OrderHolder(view);
    }

    @Override
    public void onBindHolder(RecyclerView.ViewHolder holder, int position) {
        OrderHolder orderHolder= (OrderHolder) holder;
        Order order=getList().get(position);
        orderHolder.timeText.setText(mDateFormat.format(new Date(order.getOrder_time() * 1000)));
        Glide.with(getContext()).load(order.getOrder_goods().getImage_url())
                .centerCrop().into(orderHolder.goodsImage);
        orderHolder.goodsNameText.setText(order.getOrder_goods().getGoods_name());
        SpannableStringBuilder builder=new SpannableStringBuilder();
        SpannableString priceString=SpannableString.valueOf("￥"+mPriceFormat.format(order.getOrder_amount()));
        priceString.setSpan(new AbsoluteSizeSpan(18, true), 0, priceString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.append(priceString);
        builder.append("/件(免运费)");
        orderHolder.goodsPriceText.setText(builder);
        if(order.getOrder_status()>=0 && order.getOrder_status()<mStatusArray.length)
            orderHolder.statusText.setText(mStatusArray[order.getOrder_status()]);
        else
            orderHolder.statusText.setText(null);
        orderHolder.resetFunction(order.getOrder_status(), order.getGroup_buy(),order.getGroup_order_id());
    }

    public void setOnFunctionClickListener(OnFunctionClickListener listener) {
        this.mFunctionClickListener = listener;
    }

    private class OrderHolder extends ClickableViewHolder{
        LinearLayout functionPanel;
        TextView timeText,goodsNameText,goodsPriceText,statusText;
        ImageView goodsImage;
        public OrderHolder(View itemView) {
            super(itemView);
            functionPanel= (LinearLayout) itemView.findViewById(R.id.order_function_panel);
            timeText= (TextView) itemView.findViewById(R.id.create_time_text);
            ImageView rightArrowIcon= (ImageView) itemView.findViewById(R.id.right_arrow_icon);
            rightArrowIcon.setColorFilter(ContextCompat.getColor(getContext(),R.color.light_gray));
            goodsImage= (ImageView) itemView.findViewById(R.id.goods_image);
            goodsNameText= (TextView) itemView.findViewById(R.id.goods_name_text);
            goodsPriceText= (TextView) itemView.findViewById(R.id.goods_price_text);
            statusText= (TextView) itemView.findViewById(R.id.order_status_text);
        }

        @Override
        public void onClick(View v) {
            int position=getAdapterPosition();
            if(position==RecyclerView.NO_POSITION)
                return;
            if(mFunctionClickListener==null){
                super.onClick(v);
                return;
            }
            switch (v.getId()){
    //               case R.id.order_pay_btn:
    //                   super.onClick(v);
    //                   break;
               case R.id.order_cancel_btn:
                   mFunctionClickListener.onCancel(position);
                   break;
               case R.id.order_group_detail_btn:
                   Intent intent=new Intent(getContext(), GroupDetailActivity.class);
                   intent.putExtra("id", getList().get(position).getGroup_order_id());
                   getContext().startActivity(intent);
                   //mFunctionClickListener.onGroupDetail(position);
                   break;
               case R.id.order_logistics_btn:
                   mFunctionClickListener.onLogistics(position);
                   break;
               case R.id.order_confirm_order_btn:
                   mFunctionClickListener.onConfirm(position);
                   break;
               default:
                   super.onClick(v);
           }
        }

        void resetFunction(int status,int groupBuy,String groupId){
            functionPanel.removeAllViews();
            if(status==0){
                createFunctionBtn("取消订单", R.id.order_cancel_btn, false);
                createFunctionBtn("去支付", R.id.order_pay_btn, true);
            }else {
                if(groupBuy==1 && !TextUtils.equals("0",groupId)){
                    createFunctionBtn("拼团详情",R.id.order_group_detail_btn,false);
                }
                if(status==3){
                    createFunctionBtn("查看物流",R.id.order_logistics_btn,false);
                    createFunctionBtn("确认收货",R.id.order_confirm_order_btn,true);
                }
            }
        }
        private void createFunctionBtn(String title,int id,boolean isRedBg){
            TextView textView=new TextView(getContext());
            textView.setId(id);
            textView.setText(title);
            textView.setOnClickListener(this);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
            if(isRedBg){
                textView.setBackgroundResource(R.drawable.order_function_btn_red_bg);
                textView.setTextColor(ContextCompat.getColor(getContext(),R.color.white));
            }else {
                textView.setBackgroundResource(R.drawable.order_function_btn_white_bg);
                textView.setTextColor(ContextCompat.getColor(getContext(),R.color.dark));
            }
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.leftMargin=getContext().getResources().getDimensionPixelOffset(R.dimen.order_function_btn_left_margin);
            functionPanel.addView(textView, params);
        }
    }
    public interface OnFunctionClickListener{
        void onCancel(int position);
        void onLogistics(int position);
        void onConfirm(int position);
       // void onGroupDetail(int position);
    }
}
